package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.order.OrderPayActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.PaySucceeEvent;
import com.bnlr.lrparking.event.PayTimeEvent;
import com.bnlr.lrparking.model.StallM;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;
import java.util.ArrayList;

/**
 * Created by hp on 2017/8/17.
 * 投资虚拟车位界面
 */

public class InvestActivity extends BaseActivity {

    private TextView card_number;
    private TextView tvNavTitle;
    private TextView tv_invest_btn;
    private TextView pay_money;
    private EditText tv_time;
    private RelativeLayout rely_time;
    private String id, number;
    private int sellingprice, num,monthCount;
    private String time;//
    private OptionsPickerView pvCustomOptions;
    private ArrayList<String> cardItem = new ArrayList<>();
    private int monthNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest);
        initView();
    }

    private void initView() {

        EventBus.getDefault().register(this);//
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        number = intent.getStringExtra("number");//车位编号
        sellingprice = Integer.valueOf(intent.getStringExtra("sellingprice"));
        monthCount = Integer.valueOf(intent.getStringExtra("monthCount"));
        card_number = (TextView) findViewById(R.id.card_number);
        tvNavTitle = (TextView) findViewById(R.id.tv_nav_title);
        tv_time = (EditText) findViewById(R.id.tv_time);
        tv_invest_btn = (TextView) findViewById(R.id.tv_invest_btn);
        pay_money = (TextView) findViewById(R.id.pay_money);
        tvNavTitle.setText("投资虚拟车位");
        tv_time.setOnClickListener(this);
        tv_time.addTextChangedListener(this);
        card_number.setText("车位编号：" + number);
        for (int i=0;i<monthCount;i++){
            cardItem.add(i+1+"月");
            monthNum=i;
        }
        initCustomOptionPicker();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPayTimeSuccessEvent(PayTimeEvent event) {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPaySuccessEvent(PaySucceeEvent event) {
        finish();
    }

    @Override
    public void afterTextChanged(Editable s) {
        super.afterTextChanged(s);
        time = tv_time.getText().toString();

        if (time.equals("")) {
            pay_money.setText("所需贡献值：");
            tv_invest_btn.setBackgroundResource(R.mipmap.login_disabled);
        } else {
            String strnum= time.substring(0,time.length()-1);
            num = Integer.valueOf(strnum);
            pay_money.setText("所需贡献值：" + num * sellingprice + "贡献值");
            tv_invest_btn.setBackgroundResource(R.drawable.seletor_login_btn);
            tv_invest_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setInvest(num+"");
                }
            });
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_time:
                pvCustomOptions.show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    //立即投资接口
    private void setInvest(String month) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "ApplyBuySpace");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("id", id);
            mRequest.add("month", month);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallM>(baseContext, true, StallM.class) {
                        @Override
                        public void doWork(StallM data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (code.equals("1")) {
                                    Intent intent = new Intent(InvestActivity.this, OrderPayActivity.class);
                                    intent.putExtra("orderNo", obj.getJSONObject("data").getString("orderNo"));
                                    intent.putExtra("money", obj.getJSONObject("data").getString("money"));
                                    intent.putExtra("dollor", obj.getJSONObject("data").getString("dollor"));
                                    startActivity(intent);
                                }else {
                                    CommonUtil.showToask(InvestActivity.this,obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void initCustomOptionPicker() {

        pvCustomOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                if (cardItem.size()>0) {
                    String tx = cardItem.get(options1).toString();
                    tv_time.setText(tx);
                }
            }
        }).setLayoutRes(R.layout.pickerview_time, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();
                            }
                        });
                    }
                }).isDialog(false).build();
        pvCustomOptions.setPicker(cardItem);//添加数据
    }
}
